# -*- coding: utf-8 -*-
"""
 @Time    : 2021/2/6 下午4:16
 @Author  : tgy
 
"""
from functools import partial
import socket


class Connect:
    ...

    def __init__(self, address, family=socket.AF_INET, type=socket.SOCK_STREAM):
        ...
        self.address = address
        self.family = family
        self.type = type
        self.socket = None

    def __enter__(self):
        self.socket = socket.socket(self.family, self.type)
        self.socket.connect(self.address)
        return self.socket

    def __exit__(self, exc_type, exc_val, exc_tb):
        ...
        self.socket.close()
        self.socket = None


def test01():
    ...
    with Connect(address=('127.0.0.1', 8082)) as connect:
        ...
        connect.send(b'GET / HTTP/1.1\r\n')
        connect.send(b'\r\n')
        recv_content = b''.join(iter(partial(connect.recv, 1024), b''))
        print(recv_content.decode('utf-8'))


if __name__ == '__main__':
    ...
    test01()
